package org.study.bookmanagerclientbyswing.api;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.study.bookmanagerclientbyswing.io.request.User.LoginReq;
import org.study.bookmanagerclientbyswing.io.request.User.RegisterReq;
import org.study.bookmanagerclientbyswing.io.response.RestBean;
import reactor.core.publisher.Mono;

@Component
public class UserApi {

    @Resource
    private WebClient webClient;

    private String baseUrl = "v1/user";

    public RestBean login(LoginReq req) {
        return webClient.post()
                .uri(baseUrl + "/login")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean register(RegisterReq req) {
        return webClient.post()
                .uri(baseUrl+"/register")
                .bodyValue(req)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }

    public RestBean getUserInfo(String userName) {
        return webClient.get()
                .uri(baseUrl+"/{userName}", userName)
                .retrieve()
                .bodyToMono(RestBean.class).block();
    }
}